package com.parktech.sun.domain.service.industryprealert.impl;

import com.parktech.sun.domain.model.industryprealert.IndustryPrealert;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.industryprealert.IndustryPrealertRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.industryprealert.IndustryPrealertService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-17
 * Time: 下午10:20
 */
@Service
public class IndustryPrealertServiceImpl extends BaseServiceImpl<IndustryPrealert> implements IndustryPrealertService {

    private IndustryPrealertRepository industryPrealertRepository;

    @Autowired
    public IndustryPrealertServiceImpl(IndustryPrealertRepository industryPrealertRepository) {
        super(industryPrealertRepository);
        this.industryPrealertRepository = industryPrealertRepository;
    }

    public Page search(String title, Integer publishStatus, Page page) {

        Criteria criteria = industryPrealertRepository.createCriteria();
        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }

        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryPrealertRepository.page(criteria, page);
    }

    public List<IndustryPrealert> listTop7Prealert() {
        Criteria criteria = industryPrealertRepository.createCriteria();
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        criteria.setMaxResults(7);
        return criteria.list();
    }

    public Page pageBy(Page page) {

        Criteria criteria = industryPrealertRepository.createCriteria();

        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryPrealertRepository.page(criteria, page);
    }
}
